<?php
session_start();
require_once("dbconnect.php");

ob_start();
$exeName=$_SESSION['user'];
$sql="select * from executives where exe_name='$exeName';";
//echo $sql;
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
	{
	$id=$row['id'];
	//echo $id." ";
	}
$org=$_POST['org'];
//echo $org;
$div=$_POST['divi'];

$user=$_POST['user'];
$enqtype=$_POST['enqtype'];
//echo $enqtype;
$date=dateconvert($_POST['dueDate'],1);
$techbid=($_POST['techbid']==0);
$techbid = (isset($_POST['techbid']) ) ? 1 : 0; 
//echo $techbid;
$pricebid=(isset($_POST['pricebid']) ) ? 1 : 0;
//echo $pricebid;
$prjname=$_POST['prjname'];
$probability=$_POST['prob'];
$oppname=$_POST['opprname'];
$exename=$_POST['exeName'];
//$enqnum=$_POST['enqnum'];
$currency=$_POST['curr'];
//echo $currency;
$nowdate=date('Y-m-d');
$currdate=date('d-m-Y');
list($year,$month,$day)=split('[-]',$nowdate);
//echo $day;

if($enqtype=="CP"||$enqtype=="TE"){
			$dat=$month."-".$year;
			//echo $dat."  ";
			$querystr="select * from enquiry where exe_id='$id' and enq_num like '%".$enqtype."%".$dat."%'";
			//echo $querystr;
			
		}
		else if($enqtype=="BQ"){
			$dat=$year;
			if($day=="01"&&$month=="04")
			{
				$dat=$day."-".$month."-".$year;
				$querystr="select * from enquiry where exe_id='$id' and enq_num like '%BQ%01-04-".$year."%'";
				//echo $querystr;
			}
			else
			{
			$querystr="select *	 from enquiry where exe_id='$id' and enq_num like '%BQ%".$year."%'";
			//echo $querystr;
			}
		}

if (!mysql_query($querystr))
{
die('Error for query excution: ' . mysql_error());
}
$re=mysql_query($querystr) or die(mysql_error());
//echo $re;
//$rw=mysql_fetch_assoc($re) ;
$rw=mysql_num_rows($re);
//echo "row count".($rw+1);
$enqnum=$org."/".$div."/".$enqtype."/".($rw+1)."/".$currdate;
//echo $enqnum;
$query="insert into enquiry(exe_id,org_name,division,name_of_user,enqiry_type,due_date,technical_bid,price_bid,
prj_name,probability,opp_name,exec_name,quote_corency,curr_date,enq_num) values('$id','$org','$div','$user','$enqtype','$date','$techbid',
'$pricebid','$prjname','$probability','$oppname','$exename','$currency','$nowdate','$enqnum');";
//echo $query;








if (!mysql_query($query))
{
die('Error : ' . mysql_error());
}
function dateconvert($date,$func) 
{	
if ($func == 1)
{ //insert conversion
list($day, $month, $year) = split('[/.-]', $date);
$date = "$year-$month-$day";
return $date;
}
}
$squery="select * from enquiry order by sno desc limit 1";

$qr=mysql_query($squery);
while($r=mysql_fetch_assoc($qr))
{

$enqid=$r['sno'];

}
//echo $enqid;
//echo $_POST['partno'];
foreach($_POST['prtno'] as $row=>$partno)
{
$pno=$partno;
$qnty= $_POST['qnty'][$row];
$make=$_POST['make'][$row];
$dicr=$_POST['decri'][$row];
$applic=$_POST['applic'][$row];
$spec=$_POST['spec'][$row];

$sql =  "insert into enquiry_part(enq_id,part_no, quantity, make, description, application, specification)	values('$enqid','$pno','$qnty','$make','$dicr','$applic','$spec');";
//echo $sql;
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
$msg="Successfully Inserted Enquiryform";
//echo "$row record added";
header("location:dailycall.php?msg=".$msg);
}

if($con==true) mysql_close($con);

?>